package com.padakeji.android.pworks.content;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.padakeji.android.pworks.R;

/**
 * Created by AtanL on 2015/9/14.
 */
public class UniversalImageLoaderWrapper implements PadaImageLoader {

    private Context mContext;
    private ImageLoader imageLoader;

    public UniversalImageLoaderWrapper(Context context) {
        this.mContext = context;
        imageLoader = ImageLoader.getInstance();
        configureImageLoader();
    }

    private void configureImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(mContext)
                .defaultDisplayImageOptions(options)
                .denyCacheImageMultipleSizesInMemory()
                .build();

        imageLoader.init(imageLoaderConfiguration);
    }

    /**
     *
     * @param uri
     * @param imageView
     * @param options
     * @param listener
     * @param progressListener
     */
    public void display(String uri, ImageView imageView, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) {
        imageLoader.displayImage(uri, imageView, options, listener, progressListener);
    }

    /**
     * Uri 可以是下面这几种格式。
     *
     * String imageUri = "http://site.com/image.png";      // from Web
     I String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
     * String imageUri = "content://media/external/audio/albumart/13"; // from content provider
     * String imageUri = "assets://image.png";             // from assets
     * String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
     *
     * @param uri
     * @param imageView
     */
    @Override
    public void display(String uri, ImageView imageView) {
        //显示图片的配置
        display(uri, imageView, null, null, null);
    }

    @Override
    public void display(@NonNull String imageUri, @NonNull ImageView imageView, @DrawableRes int imageOnLoading, @DrawableRes int imageOnFailure, final PadaImageLoadingListener l) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(imageOnLoading)
                .showImageOnFail(imageOnFailure)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        display(imageUri, imageView, options, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                if (l != null) {
                    l.onLoadingStart(imageUri, view);
                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                if (l != null) {
                    l.onLoadingFailed(imageUri, view, failReason.getCause());
                }
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (l != null) {
                    l.onLoadingFinish(imageUri, view, loadedImage);
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                if (l != null) {
                    l.onLoadingCancelled(imageUri, view);
                }
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {
                if (l != null) {
                    l.onLoadingProcessUpdate(imageUri, view, current, total);
                }
            }
        });
    }

}
